package cn.cslg.weatherforecast;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import cn.cslg.weatherforecast.bean.UserBean;
import cn.cslg.weatherforecast.util.DBDao;

public class LoginActivity extends AppCompatActivity {
    private EditText et1;
    private EditText et2;
    private CheckBox cb;
    private Button btn1;
    private Button btn2;
    private UserBean user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_aty_layout);
        findViews();
        setListeners();
        init();
    }

private void init() {
    SharedPreferences sp = getSharedPreferences("mypwd", MODE_PRIVATE);
    String name = sp.getString("name", "");
    String pwd = sp.getString("pwd", "");
    et1.setText(name);
    et2.setText(pwd);
    user = new UserBean(name, pwd);
}

private void setListeners() {
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = et1.getText().toString();
            String pwd = et2.getText().toString();
            DBDao dbDao = new DBDao(LoginActivity.this);
            if (dbDao.select(new UserBean(name, pwd))) {
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("isLogin", true);
                //保存密码
                if (cb.isChecked()) {
                    editor.putString("name", name);
                    editor.putString("pwd", pwd);
                    editor.putBoolean("isRemember", true);
                }
                editor.commit();
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
            } else {
                Toast.makeText(LoginActivity.this, "账号密码错误", Toast.LENGTH_SHORT).show();
            }
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            user.setName(et1.getText().toString());
            user.setPassword(et2.getText().toString());
            DBDao dbDao = new DBDao(LoginActivity.this);
            if (dbDao.select(user)) {
                Toast.makeText(LoginActivity.this, "用户已存在", Toast.LENGTH_SHORT).show();
            } else {
                dbDao.insert(user);
                Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

    private void findViews() {
        et1 = findViewById(R.id.et1);
        et2 = findViewById(R.id.et2);
        cb = findViewById(R.id.cb);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
    }
}
